//
//  TuijianViewController.swift
//  xmly
//
//  Created by 刘文超 on 2019/12/27.
//  Copyright © 2019 刘文超. All rights reserved.
//

import UIKit
import SnapKit
import SwiftMessages
class TuijianViewController: UIViewController {

    
    
    private  let collection1 = "cell1"
    private let collection2 = "cell2"
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        let collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight), collectionViewLayout: layout)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.backgroundColor = UIColor.white
        collectionView.delegate = self
        collectionView.dataSource = self
        
        
        collectionView.register(BannerWithSoreCollectionViewCell.self, forCellWithReuseIdentifier: collection1)
        collectionView.register(TuijianCaiyouLikeCollectionViewCell.self, forCellWithReuseIdentifier: collection2)
        
        
        
        
        collectionView.register(TuijianHeadCollectionReusableView.self, forSupplementaryViewOfKind:UICollectionView.elementKindSectionHeader , withReuseIdentifier: "headcell")
        collectionView.register(TuijianFootviewCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footcell")
        
        return collectionView
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        self.view.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints { (make) in
            make.width.height.equalToSuperview()
            make.center.equalToSuperview()
        }
        
        
    }
}

extension TuijianViewController: UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 0 {
            //轮播+分类+通告
                   let cell: BannerWithSoreCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: collection1, for: indexPath) as! BannerWithSoreCollectionViewCell
                   return cell
        }else{
            //猜你喜欢
                   let cell: TuijianCaiyouLikeCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: collection2, for: indexPath) as! TuijianCaiyouLikeCollectionViewCell
                   cell.delegate = self
                   return cell
        }
       
        
   }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 0 {
             return CGSize.init(width: ScreenWidth, height: 160+50+160+KBlackspace*2)
        }else{
              let flayoutWidth = (ScreenWidth-20*2-20)/3
             let flayoutHeight = (flayoutWidth*1.2+25+40)*2+25+40//加上2倍的item加上上下间距加上按钮
             return CGSize.init(width: ScreenWidth, height: flayoutHeight)
        }
       
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if section == 0 {
            return CGSize.init(width: ScreenWidth, height: 0.01)
        }
        return CGSize.init(width: ScreenWidth, height: 40)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize.init(width: ScreenWidth, height: 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader {
     
            let headCell:TuijianHeadCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headcell", for: indexPath) as! TuijianHeadCollectionReusableView
            if indexPath.section == 0 {
                headCell.isHidden = true
            }else{
                headCell.isHidden = false
            }
                return headCell;
        }else{
            let footCell:TuijianFootviewCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footcell", for: indexPath) as! TuijianFootviewCollectionReusableView
            return footCell;
        }
       
    }

   
    
}



extension TuijianViewController:TuijianCaiyouLikeCollectionViewCellDelegate{
    func TuijianItemClick(indexPathRowDict: NSDictionary) {
        let mesageView = MessageView.viewFromNib(layout: .centeredView)
        mesageView.configureDropShadow()
        mesageView.configureTheme(.warning)
        mesageView.configureContent(title: indexPathRowDict["title"] as? String ?? "", body: indexPathRowDict["content"] as? String ?? "")
        var config = SwiftMessages.defaultConfig
        config.presentationStyle = .top
        SwiftMessages.show(config: config, view: mesageView)
        
    }  
}
